package logic

import (
	"MyBlogv2/Servers/Article/article"
	"MyBlogv2/Servers/Article/internal/svc"
	define2 "MyBlogv2/blog-common/define"
	"MyBlogv2/blog-common/models"
	"context"
	"github.com/jinzhu/copier"

	"github.com/zeromicro/go-zero/core/logx"
)

type InitSearchByTitleLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewInitSearchByTitleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InitSearchByTitleLogic {
	return &InitSearchByTitleLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *InitSearchByTitleLogic) InitSearchByTitle(in *Article.ArticleRequest) (*Article.InitSearchTitleVoList, error) {
	var articleVo []models.ArticleVo
	models.Db.Model(new(models.ArticleVo)).
		Table("t_article a").
		Select("a.*,c.category_name as category_name").
		Joins("left join t_category c on a.category_id = c.id").
		Where("a.status = ?", define2.PUBLIC_ARTICLE).
		Find(&articleVo)

	var initSearchTitleVo []*Article.InitSearchTitleVo
	copier.Copy(&initSearchTitleVo, &articleVo)
	return &Article.InitSearchTitleVoList{
		InitSearchTitleVoList: initSearchTitleVo,
	}, nil
}
